//左旋字符串
class Solution {
public:
    string reverseLeftWords(string s, int n) {
        
        reverse(s.begin(),s.begin()+n);
        reverse(s.begin()+n,s.end());
        reverse(s.begin(),s.end());
        return s;
    }
};


//19. 删除链表的倒数第 N 个结点
class Solution {
public:
    ListNode* removeNthFromEnd(ListNode* head, int n) {
        //前后指针
        if(head==nullptr)
            return NULL;
        if(n==0)
        {
            return head;
        }
        if(head->next==nullptr && n==1)
        {
            return nullptr;
        }
        ListNode* prev=head;
        ListNode* Next=head;
        ListNode* father=nullptr;
        while(n-- && Next!=nullptr)
        {
            Next=Next->next;
        }

        while(Next)
        {
            Next=Next->next;
            father=prev;
            prev=prev->next;
        }
        //当Next为空的时候
        if(father==nullptr)
        {
            head=head->next;
            return head;
        }
        
        father->next=prev->next;
        return head;
        
    }
};

//24. 两两交换链表中的节点
class Solution {
public:
    ListNode* swapPairs(ListNode* head) {
        if(head==nullptr)
            return nullptr;
        
        ListNode* prev = head;
        ListNode* Next=head->next;
        if(!Next)
        {
            return head;
        }
        ListNode* newhead = Next;

        //当两个均不为空的时候，开始交换
        ListNode* tmp=nullptr;
        ListNode* father=nullptr;
        while(prev && Next )
        {
            tmp=Next->next; //Next的后续节点
            Next->next = prev;
            prev->next = tmp;
            if(father!=nullptr)
            {
                father->next=Next;
            }
            //改变完成之后，修改指向
            if(tmp!=nullptr)
            {
                father = prev;
                prev=tmp;
                Next=prev->next;
                
            }
            else
            {
                break;
            }        
        }
        return newhead;
    }
};